package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassNameShoppingCartServiceImpl
 * @Description TODO
 * @Author钰玟
 * @Date2025/2/15 下午9:21
 * @Version 1.0
 **/
@Service
public class ShoppingCartServiceImpl  implements ShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetMealMapper setMealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO 购物车数据传输对象
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前商品是否已经在购物车中
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //若存在，则更新数量
        if(list.size()>0 && list !=null){
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            //更新数据库
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //若不存在，则添加到购物车中
            if( shoppingCart.getDishId() != null){
                //说明添加的是菜品
//                shoppingCart.setSetmealId(null);
                Dish dish = dishMapper.selectById(shoppingCart.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else {
                //说明添加的是套餐
                Setmeal setmeal = setMealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }



    }
    /**
     * 查询购物车
     * @return 购物车列表
     */
    @Override
    public List<ShoppingCart> listShoppingCart() {
        BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(shoppingCart);
        return shoppingCarts;
    }
    /**
     * 清空购物车
     * @return Result
     */
    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 减少购物车
     * @param shoppingCartDTO 购物车对象
     * @return Result
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list.size() > 0 && list != null){
            if(list.get(0).getNumber() > 1){
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber()-1);
                shoppingCartMapper.updateNumberById(cart);
            }
            else {
                shoppingCartMapper.deleteById(list.get(0).getId());
            }
        }
    }


}
